package HashMap集合存储学生对象并遍历02;

/*
创建一个HashMap集合，键是学生对象(Student),值是居住地(String).存储多个键值对元素，并遍历。
如果对象的成员变量值相同，我们就认为是同一个对象
 */

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapDemo {
    public static void main(String[] args) {
        Map<Student, String> map = new HashMap<Student, String>();
        Student s1 = new Student("姜恒星", "西安");
        Student s2 = new Student("郝万杰", "西安");
        Student s3 = new Student("邓吉壕", "四川");
        Student s4 = new Student("郝万杰", "北京");

        map.put(s1, "学生一");
        map.put(s2, "学生二");
        map.put(s3, "学生三");
        map.put(s4, "学生四");

        Set<Student> keySet = map.keySet();
        for (Student key : keySet) {
            String value = map.get(key);
            System.out.println(key.getName() + "," + key.getAddress() + "," + value);

        }

        Set<Map.Entry<Student, String>> student = map.entrySet();
        for (Map.Entry<Student, String> s : student) {
            Student key = s.getKey();
            String value = s.getValue();
            System.out.println(key.getName()+","+key.getAddress()+","+value);
        }
    }
}
